require 'rubygems'
require 'hpricot'
require 'net/http'
require 'uri'

class Search
  
  attr_accessor :url, :html_results, :search_fields
  
  
  def init(params)
    @search_fields =  {'displayorder_num1_sn'=>'',
                       'displayorder_num2_givenname'=>'',
                       'displayorder_num3_initials'=>'',
                       'search_field_num1'=>params['search_field_num1'], 
                       'search_field_num10'=>'',
                       'search_field_num11'=>'',
                       'search_field_num12'=>'',
                       'search_field_num13'=>'',
                       'search_field_num14'=>'',
                       'search_field_num15'=>'',
                       'search_field_num16'=>'',
                       'search_field_num17'=>'',
                       'search_field_num2'=>params['search_field_num2'],
                       'search_field_num3'=>params['search_field_num3'],
                       'search_field_num4'=>'',
                       'search_field_num5'=>'',
                       'search_field_num6'=>'',
                       'search_field_num7'=>'',
                       'search_field_num8'=>'',
                       'search_field_num9'=>'',
                       'search_value_num1'=>params['search_value_num1'],
                       'search_value_num10'=>'',
                       'search_value_num11'=>'',
                       'search_value_num12'=>'',
                       'search_value_num13'=>'',
                       'search_value_num14'=>'',
                       'search_value_num15'=>'',
                       'search_value_num16'=>'',
                       'search_value_num17'=>'',
                       'search_value_num2'=>params['search_value_num2'],
                       'search_value_num3'=>params['search_value_num3'],
                       'search_value_num4'=>'',
                       'search_value_num5'=>'',
                       'search_value_num6'=>'',
                       'search_value_num7'=>'',
                       'search_value_num8'=>'',
                       'search_value_num9'=>'',
                       'searchtype'=>'picklist'}
  end
  
  
  def process
    @url = 'http://localhost:8090/servlet/PeopleSearch';
    
    @html_results = Net::HTTP.post_form(URI.parse(@url), @search_fields)
    results = []
    doc = Hpricot(@html_results.body)  
    doc.search("//td.listresultsdetail/a.listresult").each do |a|
      results.push({'name'=>a.inner_html, 'phone'=>a.parent.next_sibling.next_sibling.search("b").inner_html.strip})
    end
    return results
  end
  
  
# display person details link
#http://localhost:8090/servlet/PeopleSearch?dn=gmPersonId%3DTP07081%2Cou%3Dpeople%2Cdc%3Dgm%2Cdc%3Dcom&displaytype=person


  
end